$(function () {

  // 热门分类
  (function () {
    $(".hotCatg_list a").on("mouseover", function () {
      $(".hotCatg_list").find(".is-active").removeClass("is-active");
      $(this).addClass("is-active");
    });
    $(".hotCatg_list a").on("mouseout", function () {
      $(this).removeClass("is-active");
    });
  })();

  // 分类详情，菜单
  (function () {
    $('.oy-nav').addClass('is-index');
    var subNavCl = 0;
    var lastNavInx = -1;
    var navLen = $('.categorys_list>li').length;
    if (navLen % 2 != 0) {
      $('.categorys_list').append('<li data-index="' + navLen + '"><a href="javascript: void(0);"><i></i><span>&nbsp;</span></a></li>');
    }
    $('.categorys_list').on('mouseover', '>li', function () {
      clearTimeout(subNavCl);
      var index = Math.floor(parseInt($(this).data("index")) / 2);
      $('.categorys_detail').addClass('is-show');
      $('.categorys_list>li').eq(2 * lastNavInx).removeClass('is-hover');
      $('.categorys_list>li').eq(2 * lastNavInx + 1).removeClass('is-hover');
      $('.categorys_list>li').eq(2 * index).addClass('is-hover');
      $('.categorys_list>li').eq(2 * index + 1).addClass('is-hover');
      if (index == lastNavInx) return;
      var data = { list: [] };
      lastNavInx = index;
      if (CATE_ARR[2 * index]) {
        data.list.push(CATE_ARR[2 * index]);
      }
      if (CATE_ARR[2 * index + 1]) {
        data.list.push(CATE_ARR[2 * index + 1]);
      }
      $(".categorys_detail").html($.templates('#inxSubNavTmpl').render(data));
    }).on('mouseleave', '>li', function () {
      subNavCl = setTimeout(function () {
        $('.categorys_detail').removeClass('is-show');
        $('.categorys_list>li').eq(2 * lastNavInx).removeClass('is-hover');
        $('.categorys_list>li').eq(2 * lastNavInx + 1).removeClass('is-hover');
      }, 300);
    });
    $('.categorys_detail').on('mouseover', function () {
      clearTimeout(subNavCl);
      $('.categorys_detail').addClass('is-show');
    }).on('mouseleave', function () {
      subNavCl = setTimeout(function () {
        $('.categorys_detail').removeClass('is-show');
        $('.categorys_list>li').eq(2 * lastNavInx).removeClass('is-hover');
        $('.categorys_list>li').eq(2 * lastNavInx + 1).removeClass('is-hover');
      }, 300);
    });
  })();

  // banner滚动
  (function () {
    var $this = $("#slider"),
      $slides = $this.find(".slider_list>li"),
      cur = 0,
      aim = 0,
      len = $slides.length,
      circulator = 0,
      $page = $("#slider_page"),
      isIe9 = utils.isIE(9);
    $slides.css({ position: "absolute" }).eq(0).css({ display: "block" });
    if (len <= 1) {
      return false;
    };
    var pagehtml = "";
    for (var i = 0; i < len; i++) {
      pagehtml += "<li></li>";
    }
    $page.append($(pagehtml));
    $pagination = $page.find(">li");
    $pagination.eq(0).addClass('is-active');
    if (isIe9) {
      $pagination.eq(0).addClass("is-hover");
    }
    function change() {
      if (aim != cur) {
        $slides.eq(aim).fadeIn(300);
        $slides.eq(cur).fadeOut(300);
        $pagination.eq(aim).addClass("is-active");
        $pagination.eq(cur).removeClass("is-active");
        if (isIe9) {
          $pagination.eq(aim).addClass("is-hover");
          $pagination.eq(cur).removeClass("is-hover");
        }
        cur = aim;
      }
    }

    function autoPlay() {
      clearInterval(circulator);
      circulator = setInterval(function () {
        aim < len - 1 ? aim++ : aim = 0;
        change();
      }, 4000);
    }

    autoPlay();
    $pagination.each(function (i) {
      $(this).mouseenter(function () {
        aim = i;
        change();
        $(this).removeClass('is-active')
        clearInterval(circulator);
      });
      $(this).mouseleave(function () {
        $(this).addClass('is-active')
        autoPlay();
      });
    });
  })();

  // 品牌授权代理
  (function () {
    var $list = $('.AUTH_list'),
      $wrp = $('.AUTH_container'),
      $lis = $list.find('li'),
      ITEM_WIDTH = $lis.eq(0).outerWidth(),
      LIST_WIDTH = $lis.length * ITEM_WIDTH,
      ONE_PAGE_WIDTH = 5 * ITEM_WIDTH,
      curLeft = 0,
      loopAn;
    $list.append($lis.clone(true));
    $list.css({ width: 2 * LIST_WIDTH + 'px' });
    function doEffect(aimLeft) {
      $list.stop().animate({
        left: - aimLeft + 'px'
      }, 500, 'linear', function () {
        curLeft = aimLeft
      });
    }
    function auto() {
      loopAn = setInterval(function () {
        if (2 * LIST_WIDTH - curLeft <= 2 * ONE_PAGE_WIDTH) {
          $list.css({ left: - (curLeft - LIST_WIDTH) + 'px' })
          curLeft = curLeft - LIST_WIDTH
        };
        doEffect(curLeft + ONE_PAGE_WIDTH);
      }, 4000);
    }
    auto();

    $('.AUTH_prev').on('click', function () {
      if (curLeft <= ONE_PAGE_WIDTH) {
        $list.css({ left: - (curLeft + LIST_WIDTH) + 'px' })
        curLeft = curLeft + LIST_WIDTH
      }
      doEffect(curLeft - ONE_PAGE_WIDTH)
    });
    $('.AUTH_next').on('click', function () {
      if (2 * LIST_WIDTH - curLeft <= 2 * ONE_PAGE_WIDTH) {
        $list.css({ left: - (curLeft - LIST_WIDTH) + 'px' })
        curLeft = curLeft - LIST_WIDTH
      };
      doEffect(curLeft + ONE_PAGE_WIDTH);
    });
    $('.AUTH').on('mouseover', function () {
      clearInterval(loopAn)
    });
    $('.AUTH').on('mouseleave', function () {
      auto()
    });

  })();

  // 新品资讯
  function scrollLeft(settings) {
    var wrap = settings.wrap || null
    var moveLst = settings.moveLst || null
    var pageWrp = settings.pageWrp || '.scroll-page'
    var moveEle = settings.moveEle || '.scroll-lst-itm'
    var prevIcn = settings.prevIcn || '.js-prev'
    var nextIcn = settings.nextIcn || '.js-next'
    var moveNum = settings.moveNum || 1
    var autoMove = settings.autoMove || false

    if (!Boolean(moveLst)) return false;
    var $wrap = $(wrap)
    var $list = $(moveLst);
    var $listItms = $list.find(moveEle)
    var $page = null
    var $prev = null
    var $next = null
    var pageLen = undefined
    var curPage = 0
    var listLen = $listItms.length
    var listPosLeft = 0
    var listItemW = Math.ceil($wrap.width() / moveNum) // 获取列表项

    // 页码
    pageLen = Math.ceil(listLen / moveNum)
    if (pageWrp !== false) {
      $page = $(pageWrp)
      for (var i = 0; i < pageLen; i++) {
        $page.append(i !== 0 ? ('<li></li>') : ('<li class="is-active"></li>'))
      }
    }
    if (pageLen <= 1) {
      if (prevIcn) $(prevIcn).remove();
      if (nextIcn) $(nextIcn).remove();
      return false;
    }
    if (prevIcn) {
      $prev = $(prevIcn)
      $prev.on("click", function () {
        curPage == 0 ? move(pageLen - 1, "p") : move(curPage - 1, "p");
      });
    }
    if (nextIcn) {
      $next = $(nextIcn)
      $next.on("click", function () {
        curPage === pageLen - 1 ? move(0, "n") : move(curPage + 1, "n");
      });
    }
    if (pageWrp !== false) {
      $page.find(">li").each(function (index) {
        $(this).on("click", function () {
          if (!$(this).hasClass("is-active")) {
            move(index)
          }
        })
      })
    }
    // 前补后补
    var lastNum = listLen % moveNum;
    for (var i = listLen - 1; i > listLen - moveNum - 1; i--) {
      $list.prepend($listItms.eq(i).clone(true))
    }
    for (var i = 0; i < moveNum; i++) {
      $list.append($listItms.eq(i).clone(true))
    }
    var $listItms = $list.find(moveEle);
    var listLen = $listItms.length

    $list.css({ "width": listLen * listItemW + 'px', "left": -listItemW * moveNum + 'px' });
    $listItms.css({ "width": listItemW + "px" });

    //
    var clock = 0

    function setAuto() {
      clock = setInterval(function () {
        if (curPage >= pageLen - 1) {
          move(0, "n");
        } else {
          move(curPage + 1, "n");
        }
      }, autoMove || 3000)
    }

    if (autoMove) {
      setAuto()
      $wrap.on('mouseover', function () {
        clearInterval(clock)
      })
      $wrap.on('mouseleave', function () {
        setAuto()
      })
    }

    function move(aimPage, dir) {
      if (dir == "n" && aimPage == 0) {
        $list.css({ "left": lastNum == 0 ? 0 : (lastNum - moveNum) * listItemW });
      } else if (dir == "p" && aimPage == pageLen - 1) {
        $list.css({ "left": -(listLen - moveNum) * listItemW + 'px' });
      }
      listPosLeft = -(aimPage + 1) * moveNum * listItemW;
      $list.animate({
        left: listPosLeft + 'px'
      })
      curPage = aimPage;
      if (pageWrp !== false) {
        $page.find(">li").eq(curPage).addClass("is-active").siblings(".is-active").removeClass("is-active")
      }
    }

  };
  setTimeout(function () {
    scrollLeft({
      wrap: ".newPro_listWrp",
      moveLst: ".newPro_list",
      pageWrp: false,
      moveEle: '>li',
      prevIcn: ".newPro_prev",
      nextIcn: ".newPro_next",
      moveNum: 2,
      autoMove: 4000
    });
  }, 0);

  // 解决方案
  (function () {
    var $wrp = $(".solution_panel.is-slide"),
      $wrds = $wrp.find(">p");
    $ul = $wrp.find("ul"),
      itmW = $ul.width() / 3;
    $ul.append($ul.find("li").eq(0).clone(true));
    var cur = 0, aim = 0, clock = 0;
    function change() {
      if (aim != cur) {
        if (cur == 0) {
          $ul.css({ "left": "0" });
        }
        $ul.stop().animate({
          left: -(cur + 1) * itmW + 'px'
        }, 300, function () {
          $(".solution_slide").attr("href", oy_global.ctx + (aim == 0 ? "/page/solution_index.html" : "/page/rohm_spice.html"));
        });
        $wrds.eq(aim).fadeIn(300);
        $wrds.eq(cur).fadeOut(300);
        cur = aim;
      }
    }
    function setAuto() {
      clock = setInterval(function () {
        aim = cur == 0 ? 1 : 0;
        change();
      }, 4000);
    }
    $wrp.on("mouseover", function () {
      clearInterval(clock)
    });
    $wrp.on('mouseleave', function () {
      setAuto()
    });
    setAuto();
  })();

  // 侧边栏广告链接
  $(".invite_link .icon-x").on("click", function () {
    $(".invite_link").remove();
  });

  // 资源文件最新显示
  var latestFileResourceTime = PAGE_DATA.latestFileResourceTime;
  var latestFileResourceTimeCookie = Cookies.get('LATEST_FILE_RESOURCE_TIME')
  if (latestFileResourceTime != '') {
    if (latestFileResourceTimeCookie == undefined || latestFileResourceTime > latestFileResourceTimeCookie) {
      $('#newFileResourceTag').show()
    }
  }
  $('#newFileResourceHref').click(function () {
    if (latestFileResourceTime != '' && (latestFileResourceTimeCookie == undefined || latestFileResourceTime > latestFileResourceTimeCookie)) {
      Cookies.set('LATEST_FILE_RESOURCE_TIME', latestFileResourceTime, { expires: 99999 });
    }
  })
});